
package crl.encrypt
{
	import com.swfwire.decompiler.abc.ABCFile;
	import com.swfwire.decompiler.abc.tokens.ConstantPoolToken;
	
	import crl.encrypt.ext.PlayerGlobalDefine;
	
	import flash.utils.Dictionary;
	
	
	/**
	 * 
	 * @author crl
	 * 
	 */	
	public class ReplaceCoder
	{
		public var codes:Array=['µ','£','Æ','æ','è','ê','é','ë','ò','ô','õ'];
		private var len:int;
		
		
		public var replacedDic:Dictionary;
		
		private var constantPool:ConstantPoolToken;
		private var innerStrings:Dictionary;
		private var globalStrings:Vector.<String>;
		public var totalStrings:Dictionary;
		public function ReplaceCoder(innerStrings:Dictionary)
		{
			len=codes.length;
			replacedDic=new Dictionary();
			
			if(this.globalStrings==null){
				var globals:Vector.<String>=PlayerGlobalDefine.instance.read();
				
				globals.push("ByteArrayAsset");
				globals.push("IFlexAsset");
				globals.push("void");
				globals.push("Void");
				globals.push("NameUtil");
				globals.push("FlexBitmap");
				globals.push("IFlexDisplayObject");
				globals.push("BitmapAsset");
				globals.push("IRepeaterClient");
				globals.push("mx.core");
				globals.push("mx.utils");
				globals.push("private");
				globals.push("internal");
				globals.push("protected");
				globals.push("set");
				globals.push("get");
				globals.push("http://www.adobe.com/2006/flex/mx/internal");
				globals.push("elements");
				globals.push("@");
				globals.push("localName");
				globals.push("name");
				globals.push("label");
				globals.push("text");
				globals.push("child");
				globals.push("children");
				globals.push("debug");
				globals.push("config");
				//globals.push("t0");
				//globalStrings.push("IFlexAsset");
				this.globalStrings=globals;
			}
			this.innerStrings=innerStrings;
			
			totalStrings=innerStrings;
			
			for each(var item:* in globalStrings){
					if(isNaN(item)==false)continue;
					if(!totalStrings[item])	totalStrings[item]=1;
				
			}
		}
		
		public  function replaceName(name:String):String{
			
			if(totalStrings[name]){
				return name;
			}

			var hasName:String=replacedDic[name];
			if(hasName==null){
				
				//if(name.substr(0,2)=="f_")return name;
				
				hasName=get();
				replacedDic[name]=hasName;
			}
			//trace(name+":\t\t\t\t"+hasName);
			EncryptApp.logHandle(name+":"+hasName);
			return hasName;
		}
		
		private var position:int=0;
		public function get():String{
			var result:Array=[];
			
			var temp:int;
			var index:int=position;
			do{
				temp=index;
				index=temp/len;
				result.push(codes[temp%len]);
			}while(index)
			position++;
			return result.join("");
		}
	}
}